﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ArchivesAdmin.Data.Repository;
using ArchivesAdmin.Entity.TrainingManage;
using ArchivesAdmin.Util;
using ArchivesAdmin.Util.Model;
using ArchivesAdmin.Util.Extension;
using ArchivesAdmin.Model.Param.TrainingManage;
using System.Linq.Expressions;

namespace ArchivesAdmin.Service.TrainingManage
{
    public class TeacherService : RepositoryFactory
    {
        #region 获取数据
        public async Task<List<TeacherEntity>> GetList(TeacherListParam param)
        {
            var expression = ListFilter(param);
            var list = await this.BaseRepository().FindList(expression);
            return list.ToList();
        }

        public async Task<List<TeacherEntity>> GetPageList(TeacherListParam param, Pagination pagination)
        {
            var expression = ListFilter(param);
            var list = await this.BaseRepository().FindList(expression, pagination);
            return list.ToList();
        }

        public async Task<TeacherEntity> GetEntity(long id)
        {
            return await this.BaseRepository().FindEntity<TeacherEntity>(id);
        }

        public async Task<TeacherEntity> GetEntity(string name)
        {
            return await this.BaseRepository().FindEntity<TeacherEntity>(p => p.Name == name);
        }
  
        #endregion

        #region 提交数据
        public async Task UpdateTeacher(TeacherEntity entity)
        {
            await this.BaseRepository().Update(entity);
        }

        public async Task SaveForm(TeacherEntity entity)
        {
            if (entity.Id.IsNullOrZero())
            {
                await entity.Create();
                await this.BaseRepository().Insert<TeacherEntity>(entity);
            }
            else
            {
                await entity.Modify();
                await this.BaseRepository().Update<TeacherEntity>(entity);
            }
        }

        public async Task DeleteForm(string ids)
        {
            long[] idArr = TextHelper.SplitToArray<long>(ids, ',');
            await this.BaseRepository().Delete<TeacherEntity>(idArr);
        }

        #endregion

        #region 私有方法
        private Expression<Func<TeacherEntity, bool>> ListFilter(TeacherListParam param)
        {
            var expression = LinqExtensions.True<TeacherEntity>();
            if (param != null)
            {
                if (!string.IsNullOrEmpty(param.Name))
                {
                    expression = expression.And(t => t.Name.Contains(param.Name));
                }               
                if (param.Age > 0)
                {
                    expression = expression.And(t => t.Age == param.Age);
                }

                //if (!string.IsNullOrEmpty(param.Birthday.ParseToString()))
                //{
                //    expression = expression.And(t => t.Birthday >= param.Birthday);
                //}
                
            }
            return expression;
        }
        #endregion
    }
}
